package com.java.controller;


import com.java.entity.Capability;
import com.java.entity.Profile;
import com.java.mapper.CapabilityMapper;
import com.java.mapper.ProfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/profile")
public class ProfileController {

    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private CapabilityMapper capabilityMapper;
    @GetMapping("/test")
    public String test() {
        return "ok";
    }

    /**
     * 获取所有模板和能力点
     * @return
     */
    @GetMapping("/list")
    public List<Map<String, Object>> listProfiles() {
        List<Profile> profiles = profileMapper.selectAll();
        List<Map<String, Object>> result = new ArrayList<>();
        for (Profile p : profiles) {
            Map<String, Object> map = new HashMap<>();
            map.put("profile", p);
            List<Capability> caps = capabilityMapper.selectByProfileId(p.getId());
            map.put("capabilities", caps);
            result.add(map);
        }
        return result;
    }

    /**
     * 新增模板 + 能力点
     * @param body
     * @return
     */
    @PostMapping("/add")
    public String addProfileWithCapabilities(@RequestBody Map<String, Object> body) {
        Profile p = new Profile();
        p.setName((String) body.get("name"));
        p.setDeviceType((String) body.get("deviceType"));
        p.setProtocol((String) body.getOrDefault("protocol", "MQTT"));
        profileMapper.insert(p);

        List<Map<String, Object>> caps = (List<Map<String, Object>>) body.get("capabilities");
        int sort = 0;
        for (Map<String, Object> capMap : caps) {
            Capability cap = new Capability();
            cap.setProfileId(p.getId());
            cap.setName((String) capMap.get("name"));
            cap.setDescription((String) capMap.get("description"));
            cap.setUnit((String) capMap.get("unit"));
            cap.setSchemaJson((String) capMap.get("schemaJson"));
            cap.setSort(sort++);
            capabilityMapper.insert(cap);
        }
        return "ok";
    }

    /**
     * 查询单模板详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Map<String, Object> getProfile(@PathVariable Long id) {
        Profile p = profileMapper.selectById(id);
        List<Capability> caps = capabilityMapper.selectByProfileId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("profile", p);
        map.put("capabilities", caps);
        return map;
    }

    /**
     * 删除模板 + 能力点
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String deleteProfile(@PathVariable Long id) {
        capabilityMapper.deleteByProfileId(id); // 先删子表
        profileMapper.deleteById(id);           // 再删主表
        return "ok";
    }

    /**
     * 更新设备 + 能力点
     * @param id
     * @param body
     * @return
     */
    @PutMapping("/{id}")
    public String updateProfileAndCapabilities(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        Profile p = new Profile();
        p.setId(id);
        p.setName((String) body.get("name"));
        p.setDeviceType((String) body.get("deviceType"));
        p.setProtocol((String) body.getOrDefault("protocol", "MQTT"));
        profileMapper.updateById(p);

        // 先删旧能力点
        capabilityMapper.deleteByProfileId(id);
        // 新增能力点
        List<Map<String, Object>> caps = (List<Map<String, Object>>) body.get("capabilities");
        int sort = 0;
        for (Map<String, Object> capMap : caps) {
            Capability cap = new Capability();
            cap.setProfileId(id);
            cap.setName((String) capMap.get("name"));
            cap.setDescription((String) capMap.get("description"));
            cap.setUnit((String) capMap.get("unit"));
            cap.setSchemaJson((String) capMap.get("schemaJson"));
            cap.setSort(sort++);
            capabilityMapper.insert(cap);
        }
        return "ok";
    }
}
